package com.sjsoft.webservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sjsoft.constant.Constant;
import com.sjsoft.model.CSubCategory;
import com.sjsoft.model.SmsItem;
import com.sjsoft.model.messageItem;
import com.sjsoft.model.videoItem;


public class UtilsJson implements Constant{

	public final static String DEFAULT_SERVER = "http://vipmobi.net:7001/MOBILE/ServiceJsonService?wsdl";
	public final static String DOWNLOAD_SERVER = "http://vipmobi.net:7001/MOBILE/download?";
	
	public static final String NAMESPACE = "http://JsonUtils/";
	
	public final static String DEFAULT_USER = "chat";
	public final static String APP_NAME = "chat";
	
	public static final String ACTION_VIDEO="getJsonVideo";
	
	
	public static String getExtendId()
	{
		String server = DEFAULT_SERVER;
		try {
			URL u = new URL(server);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setUseCaches(false);
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(30000);
			String xml = "<soap:Envelope "
					+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
					+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
					+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
					+ "  <soap:Body> \r\n"
					+ " <getJsonCategoryExtend xmlns='http://JsonUtils/'> \r\n"
					+ "   <parentId xmlns=''>-1</parentId> \r\n"
					+ "   <username xmlns=''>"+DEFAULT_USER+"</username> \r\n"					
					+ "   <status xmlns=''>1</status> \r\n"
					+ "    </getJsonCategoryExtend> \r\n" + "  </soap:Body> \r\n"
					+ "</soap:Envelope> \r\n";
			connection.setRequestMethod("POST");
			connection.setRequestProperty("soapAction", "");
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			int len = xml.length();
			connection.setRequestProperty("Content-Length",
					Integer.toString(len));
			connection.connect();
			OutputStream out = connection.getOutputStream();
			OutputStreamWriter wout = new OutputStreamWriter(out);
			wout.write(xml);
			wout.flush();
			wout.close();

			StringBuffer response = new StringBuffer();
			InputStream inStream = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inStream));
			String line;
			while ((line = rd.readLine()) != null) {
				response.append(line);
				String sReturn = response.toString();
				
				
				int nPos = sReturn.indexOf("[");
				int nLast = sReturn.lastIndexOf("]");

				return sReturn.substring(nPos, nLast + 1);
			}
		} catch (IOException e) {
			return null;
		}
		return null;
	}
	
	
	public static String getCategoryVideo(String sExtendId)
	{
		String server = DEFAULT_SERVER;
		try {
			URL u = new URL(server);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setUseCaches(false);
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(30000);
			
			String xml = "<soap:Envelope "
					+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
					+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
					+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
					+ "  <soap:Body> \r\n"
					+ "    <getJsonCategoryExtend xmlns='http://JsonUtils/'> \r\n"
					+ "   <parentId xmlns=''>" + sExtendId + "</parentId> \r\n"					
					+ "   <username xmlns=''>"+DEFAULT_USER+"</username> \r\n" 
					+ "   <status xmlns=''>1</status> \r\n"
					+ "    </getJsonCategoryExtend> \r\n"
					+ "  </soap:Body> \r\n" 
					+ "</soap:Envelope> \r\n";
						
			connection.setRequestMethod("POST");
			connection.setRequestProperty("soapAction", "");
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			int len = xml.length();
			connection.setRequestProperty("Content-Length",
					Integer.toString(len));
			connection.connect();
			OutputStream out = connection.getOutputStream();
			OutputStreamWriter wout = new OutputStreamWriter(out);
			wout.write(xml);
			wout.flush();
			wout.close();

			StringBuffer response = new StringBuffer();
			InputStream inStream = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inStream));
			String line;
			while ((line = rd.readLine()) != null) {
				response.append(line);
				String sReturn = response.toString();
				int nPos = sReturn.indexOf("[");
				int nLast = sReturn.lastIndexOf("]");

				return sReturn.substring(nPos, nLast + 1);
			}
		} catch (IOException e) {
			return null;
		}
		return null;
	}
	
	public static String getItemsVideo(String sExtendId,int minRow, int maxRow)
	{
		String server = DEFAULT_SERVER;
		try {
			URL u = new URL(server);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setUseCaches(false);
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(30000);	
			
			String xml = "<soap:Envelope "
					+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
					+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
					+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
					+ "  <soap:Body> \r\n"
					+ "    <getJsonVideo xmlns='http://JsonUtils/'> \r\n"
					+ "   <extendId xmlns=''>" + sExtendId + "</extendId> \r\n"					
					+ "   <username xmlns=''>"+DEFAULT_USER+"</username> \r\n" 
					+ "   <status xmlns=''>1</status> \r\n"
					+ "			<minRow xmlns=''>" + minRow + "</minRow>"
					+ "			<maxRow xmlns=''>" + maxRow + "</maxRow>" 
					+ "    </getJsonVideo> \r\n"
					+ "  </soap:Body> \r\n" 
					+ "</soap:Envelope> \r\n";
						
			connection.setRequestMethod("POST");
			connection.setRequestProperty("soapAction", "");
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			int len = xml.length();
			connection.setRequestProperty("Content-Length",
					Integer.toString(len));
			connection.connect();
			OutputStream out = connection.getOutputStream();
			OutputStreamWriter wout = new OutputStreamWriter(out);
			wout.write(xml);
			wout.flush();
			wout.close();

			StringBuffer response = new StringBuffer();
			InputStream inStream = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inStream));
			String line;
			while ((line = rd.readLine()) != null) {
				response.append(line);
				String sReturn = response.toString();
				int nPos = sReturn.indexOf("[");
				int nLast = sReturn.lastIndexOf("]");

				return sReturn.substring(nPos, nLast + 1);
			}
		} catch (IOException e) {
			return null;
		}
		return null;
	}
	
	
	public static String getListChat(String profileId)
	{
		String server = DEFAULT_SERVER;
		try {
			URL u = new URL(server);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setUseCaches(false);
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(30000);
			
			String xml = "<soap:Envelope "
					+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
					+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
					+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
					+ "  <soap:Body> \r\n"
					+ "    <getChatVideo xmlns='http://JsonUtils/'> \r\n"
					+ "     <profileId xmlns=''>" + profileId + "</profileId> \r\n"							
					+ "     <status xmlns=''>1</status> \r\n"
					+ "     <username xmlns=''>"+DEFAULT_USER+"</username> \r\n"
					+ "    </getChatVideo> \r\n"
					+ "  </soap:Body> \r\n" 
					+ "</soap:Envelope> \r\n";
						
			connection.setRequestMethod("POST");
			connection.setRequestProperty("soapAction", "");
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			int len = xml.length();
			connection.setRequestProperty("Content-Length",
					Integer.toString(len));
			connection.connect();
			OutputStream out = connection.getOutputStream();
			OutputStreamWriter wout = new OutputStreamWriter(out);
			wout.write(xml);
			wout.flush();
			wout.close();

			StringBuffer response = new StringBuffer();
			InputStream inStream = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inStream));
			String line;
			while ((line = rd.readLine()) != null) {
				response.append(line);
				String sReturn = response.toString();
				int nPos = sReturn.indexOf("[");
				int nLast = sReturn.lastIndexOf("]");

				return sReturn.substring(nPos, nLast + 1);
			}
		} catch (IOException e) {
			return null;
		}
		return null;
	}

	public static videoItem getListMessageFromJson(String sJson,videoItem itemVideo)
	{
	
		ArrayList<messageItem> arrMsg = new ArrayList<messageItem>();		
		try {
			JSONArray jsonArray = new JSONArray(sJson);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				
				itemVideo.setName(jsonObject.getString(KEY_CHAT_USER));		
				String sAvatar = jsonObject.getString(KEY_CHAT_AVATAR);
				itemVideo.setIdIcon(sAvatar);
				itemVideo.setAddress(jsonObject.getString(KEY_CHAT_ADDRESS));
				itemVideo.setAge(jsonObject.getString(KEY_CHAT_AGE));
				itemVideo.setHeight(jsonObject.getString(KEY_CHAT_HEIGHT));
				itemVideo.setWeight(jsonObject.getString(KEY_CHAT_WEIGHT));
				itemVideo.setDesc(jsonObject.getString(KEY_CHAT_DESC));
				String sListMessage = jsonObject.getString(KEY_CHAT_CONTENT_LIST);
				JSONArray jsonListMessage = new JSONArray(sListMessage);
				for (int j = 0; j < jsonListMessage.length(); j++) {
					
					JSONObject jsonMes = jsonListMessage.getJSONObject(j);
					String sContent = jsonMes.getString(KEY_CHAT_CONTENT);
					messageItem item = new messageItem(sContent,sAvatar,0); 				
					arrMsg.add(item);
				}
				if(itemVideo != null)
					itemVideo.setMessage(arrMsg);
			}

		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		
		return itemVideo;
	}
	
	
	public static ArrayList<videoItem> getListVideoFromJson(String sJson)
	{
		ArrayList<videoItem> arrVideo = new ArrayList<videoItem>();
		if(sJson == null)
			return arrVideo;
		try {
			JSONArray jsonArray = new JSONArray(sJson);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);

				String sName = jsonObject.getString(KEY_VIDEO_NAME);
				String sHttp = jsonObject.getString(KEY_VIDEO_HTTP);
				String sThumb = jsonObject.getString(KEY_VIDEO_THUMBNAIL);
				String sProfileId = jsonObject.getString(KEY_VIDEO_PROFILE_ID);
				String sExtendId = jsonObject.getString(KEY_EXTEND_ID);
				String sAmount = jsonObject.getString(KEY_VIDEO_AMOUNT);
				String sVideoId = jsonObject.getString(KEY_VIDEO_ID);
				int nAmount = 0;
				if(sAmount != null && !sAmount.equalsIgnoreCase("null"))
				 nAmount = Integer.parseInt(sAmount);
				videoItem item = new videoItem(sName,sExtendId,sThumb,sHttp,sProfileId,nAmount,sVideoId);
				arrVideo.add(item);
			}

		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		
		return arrVideo;
	}
	public static ArrayList<CSubCategory> getListSubByExtendId(String sJson) {
		if(sJson == null)
			return null;
		ArrayList<CSubCategory> arr = new ArrayList<CSubCategory>();	
		try {
			JSONArray jsonArray = new JSONArray(sJson);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);

				CSubCategory subCategory = new CSubCategory();
				subCategory.setName(jsonObject.getString(KEY_EXTEND_NAME));
				
				subCategory.setEtendId(jsonObject.getString(KEY_EXTEND_ID));
				subCategory
						.setCategoryId(jsonObject.getString(KEY_CATEGORY_ID));
				subCategory
						.setCommand(jsonObject.getString(KEY_EXTEND_COMMAND));				
				String des = jsonObject.getString(KEY_DESCRIPTION);
				if (!des.equalsIgnoreCase("null")) {
					subCategory.setDescription(des);
				} else {
					subCategory.setDescription("");
				}
				String order = jsonObject.getString(KEY_EXTEND_ORDER);
				if (order.equalsIgnoreCase("null")) {
					subCategory.setExtendOrder(0);
				} else {
					subCategory.setExtendOrder(Integer.parseInt(order));
				}
				arr.add(subCategory);
			}

		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

		return arr;
	}
	
	public static String getInforBilling(String imei,String extendId,String itemId,int amount)
	{
		String server = DEFAULT_SERVER;
		try {
			URL u = new URL(server);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setUseCaches(false);
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(30000);		
			
			String xml = "<soap:Envelope "
					+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
					+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
					+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
					+ "  <soap:Body> \r\n"
					+ "    <billing xmlns='http://JsonUtils/'> \r\n"
					+ "   <deviceImei xmlns=''>" + imei + "</deviceImei> \r\n"
					+ "   <appName xmlns=''>" + APP_NAME + "</appName> \r\n"					
					+ "   <username xmlns=''>"+DEFAULT_USER+"</username> \r\n"
					+ "   <extendId xmlns=''>"+extendId+"</extendId> \r\n"
					+ "   <itemId xmlns=''>"+itemId+"</itemId> \r\n"
					+ "   <amount xmlns=''>"+amount+"</amount> \r\n"				
					+ "    </billing> \r\n"
					+ "  </soap:Body> \r\n" 
					+ "</soap:Envelope> \r\n";
						
			connection.setRequestMethod("POST");
			connection.setRequestProperty("soapAction", "");
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			int len = xml.length();
			connection.setRequestProperty("Content-Length",
					Integer.toString(len));
			connection.connect();
			OutputStream out = connection.getOutputStream();
			OutputStreamWriter wout = new OutputStreamWriter(out);
			wout.write(xml);
			wout.flush();
			wout.close();

			StringBuffer response = new StringBuffer();
			InputStream inStream = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inStream));
			String line;
			while ((line = rd.readLine()) != null) {
				response.append(line);
				String sReturn = response.toString();
				int nPos = sReturn.indexOf("{");
				int nLast = sReturn.lastIndexOf("}");

				return "["+sReturn.substring(nPos, nLast + 1) +"]";
			}
		} catch (IOException e) {
			return null;
		}
		return null;
	}
	
	
	public static String payment(String imei,String paytype,String sCodeCard,String sSerialCard,String sTelco, String profixName, String profixCode)
	{
		String server = DEFAULT_SERVER;
		try {
			URL u = new URL(server);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setUseCaches(false);
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(30000);		
			
			String xml = "<soap:Envelope "
					+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
					+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
					+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
					+ "  <soap:Body> \r\n"
					+ "    <payment xmlns='http://JsonUtils/'> \r\n"
					+ "   <deviceImei xmlns=''>" + imei + "</deviceImei> \r\n"
					+ "   <appName xmlns=''>" + APP_NAME + "</appName> \r\n"					
					+ "   <username xmlns=''>"+DEFAULT_USER+"</username> \r\n"
					+ "   <paymentType xmlns=''>"+paytype+"</paymentType> \r\n"
					+ "   <codeCard xmlns=''>"+sCodeCard+"</codeCard> \r\n"
					+ "   <serialCard xmlns=''>"+sSerialCard+"</serialCard> \r\n"
					+ "   <telco xmlns=''>"+sTelco+"</telco> \r\n"
					+ "   <prefixName xmlns=''>"+profixName+"</prefixName> \r\n"
					+ "   <prefixCode xmlns=''>"+profixCode+"</prefixCode> \r\n"
					+ "    </payment> \r\n"
					+ "  </soap:Body> \r\n" 
					+ "</soap:Envelope> \r\n";
						
			connection.setRequestMethod("POST");
			connection.setRequestProperty("soapAction", "");
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			int len = xml.length();
			connection.setRequestProperty("Content-Length",
					Integer.toString(len));
			connection.connect();
			OutputStream out = connection.getOutputStream();
			OutputStreamWriter wout = new OutputStreamWriter(out);
			wout.write(xml);
			wout.flush();
			wout.close();

			StringBuffer response = new StringBuffer();
			InputStream inStream = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inStream));
			String line;
			while ((line = rd.readLine()) != null) {
				response.append(line);
				String sReturn = response.toString();
				int nPos = sReturn.indexOf("{");
				int nLast = sReturn.lastIndexOf("}");

				return "["+sReturn.substring(nPos, nLast + 1) +"]";
			}
		} catch (IOException e) {
			return null;
		}
		return null;
	}
	
	public static String ExpiredDate(String currentVersion, String imei)
	{
		String server = DEFAULT_SERVER;
		try {
			URL u = new URL(server);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setUseCaches(false);
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(30000);
			String xml = "<soap:Envelope "
					+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
					+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
					+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
					+ "  <soap:Body> \r\n"
					+ " <deviceConnect xmlns='http://JsonUtils/'> \r\n"
					+ "	<deviceImei  xmlns=''>" + imei
					+ "</deviceImei> \r\n"
					+ "	<appName  xmlns=''>" + APP_NAME + "</appName> \r\n"
					+ "	<OS  xmlns=''>" + "Android" + "</OS > \r\n"
					+ "	<currentVersion  xmlns=''>" + currentVersion
					+ "</currentVersion > \r\n"
					+ " <username xmlns=''>"+ DEFAULT_USER + "</username> \r\n"	
					+ " <checkVersion xmlns=''>true</checkVersion> \r\n"
					+ " <addExpiredDate xmlns=''>true</addExpiredDate> \r\n"
					+ "    </deviceConnect> \r\n" + "  </soap:Body> \r\n"
					+ "</soap:Envelope> \r\n";
			connection.setRequestMethod("POST");
			connection.setRequestProperty("soapAction", "");
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			int len = xml.length();
			connection.setRequestProperty("Content-Length",
					Integer.toString(len));
			connection.connect();
			OutputStream out = connection.getOutputStream();
			OutputStreamWriter wout = new OutputStreamWriter(out);
			wout.write(xml);
			wout.flush();
			wout.close();

			StringBuffer response = new StringBuffer();
			InputStream inStream = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inStream));
			String line;
			while ((line = rd.readLine()) != null) {
				response.append(line);
				String sReturn = response.toString();
				int nPos = sReturn.indexOf("{");
				int nLast = sReturn.lastIndexOf("}");

				return "["+sReturn.substring(nPos, nLast + 1) +"]";
				
			}
		} catch (IOException e) {
			return "";
		}
		return "";
	}
	
	public static int getTotalAmount(String sJson)
	{
		int nAmount = 0 ;
		try {
			JSONArray jsonArray = new JSONArray(sJson);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String sAmount =  jsonObject.getString(KEY_VIDEO_AMOUNT);
				try {
					nAmount = Integer.parseInt(sAmount);
				} catch (Exception e) {
					// TODO: handle exception
					nAmount = 0;
				}
			}

		} catch (JSONException e) {
			e.printStackTrace();			
		}

		return nAmount;
	}
	
	
	public static String updateVersion(String currentVersion, String imei) {
		String server = DEFAULT_SERVER;
		
		StringBuffer response = null;
		try {
			URL u = new URL(server);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setUseCaches(false);
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(30000);
			String xml = "<soap:Envelope "
					+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
					+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
					+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
					+ "  <soap:Body> \r\n"
					+ " <deviceConnect xmlns='http://JsonUtils/'> \r\n"
					+ "	<deviceImei  xmlns=''>" + imei
					+ "</deviceImei> \r\n"
					+ "	<appName  xmlns=''>" + APP_NAME + "</appName> \r\n"
					+ "	<OS  xmlns=''>" + "Android" + "</OS > \r\n"
					+ "	<currentVersion  xmlns=''>" + currentVersion
					+ "</currentVersion > \r\n"
					+ " <username xmlns=''>"+ DEFAULT_USER + "</username> \r\n"	
					+ " <checkVersion xmlns=''>true</checkVersion> \r\n"					
					+ "    </deviceConnect> \r\n" + "  </soap:Body> \r\n"
					+ "</soap:Envelope> \r\n";
			connection.setRequestMethod("POST");
			connection.setRequestProperty("soapAction", "");
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			int len = xml.length();
			connection.setRequestProperty("Content-Length",
					Integer.toString(len));
			connection.connect();
			OutputStream out = connection.getOutputStream();
			OutputStreamWriter wout = new OutputStreamWriter(out);
			wout.write(xml);
			wout.flush();
			wout.close();

			response = new StringBuffer();
			InputStream inStream = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inStream));
			String line;
			while ((line = rd.readLine()) != null) {
				response.append(line);			
				String sReturn = response.toString();
				int nPos = sReturn.indexOf("{");
				int nLast = sReturn.lastIndexOf("}");

				return "["+sReturn.substring(nPos, nLast + 1) +"]";
			}
		} catch (IOException e) {
			return null;
		}
		
			return null;
		
	}
	
	public static String getListPrice()
	{
		String server = DEFAULT_SERVER;
		try {
			URL u = new URL(server);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setUseCaches(false);
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(30000);
			
			String xml = "<soap:Envelope "
					+ "xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/' "
					+ "xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance' "
					+ "xmlns:xsd= 'http://www.w3.org/2001/XMLSchema'> \r\n"
					+ "  <soap:Body> \r\n"
					+ "    <getListPrice xmlns='http://JsonUtils/'> \r\n"
					+ "	<appName  xmlns=''>" + APP_NAME + "</appName> \r\n"
					+ "     <username xmlns=''>"+DEFAULT_USER+"</username> \r\n"
					+ "    </getListPrice> \r\n"
					+ "  </soap:Body> \r\n" 
					+ "</soap:Envelope> \r\n";
						
			connection.setRequestMethod("POST");
			connection.setRequestProperty("soapAction", "");
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			int len = xml.length();
			connection.setRequestProperty("Content-Length",
					Integer.toString(len));
			connection.connect();
			OutputStream out = connection.getOutputStream();
			OutputStreamWriter wout = new OutputStreamWriter(out);
			wout.write(xml);
			wout.flush();
			wout.close();

			StringBuffer response = new StringBuffer();
			InputStream inStream = connection.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					inStream));
			String line;
			while ((line = rd.readLine()) != null) {
				response.append(line);
				String sReturn = response.toString();
				int nPos = sReturn.indexOf("{");
				int nLast = sReturn.lastIndexOf("}");

				return "["+sReturn.substring(nPos, nLast + 1) +"]";
			}
		} catch (IOException e) {
			return null;
		}
		return null;
	}
	
	public static ArrayList<SmsItem> getListPriceByJson(String jSon)
	{
		if(jSon == null)
			return null;
		ArrayList<SmsItem> arr = new ArrayList<SmsItem>();
		
		try {
			JSONArray jsonArray = new JSONArray(jSon);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				
				String sPrefixCode = jsonObject.getString("prefixCode");
				String sList = jsonObject.getString("moneyRatioList");
				
				JSONArray jsonList = new JSONArray(sList);
				for (int j = 0; j < jsonList.length(); j++) {
					JSONObject jsonItem = jsonList.getJSONObject(j);
					String sName = jsonItem.getString("name");
					String sNumber = jsonItem.getString("value");
					
					int nType = 0;
					if(sName.contains("Đầu 5"))
						nType = 1;
					else if(sName.contains("Đầu 6"))
						nType = 2;
					else if(sName.contains("Đầu 7"))
						nType = 3;
					if(nType > 0)
					{
						SmsItem item = new SmsItem(sNumber, sPrefixCode, nType);
						arr.add(item);
					}
					
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return arr;
	}
}